package com.hdoit.ares.web.acc.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.ares.logical.acc.model.AwardSetting;
import com.hdoit.ares.logical.acc.service.IAwardSettingService;
import com.hdoit.ares.logical.viewmodel.acc.AwardSettingSearch;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月28日 19:34:59
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/acc/awardSetting/")
public class AwardSettingController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/acc/awardSetting/";
    }

    private IAwardSettingService awardSettingService;
    public IAwardSettingService getAwardSettingService() {
        return awardSettingService;
    }
    @Resource
    public void setAwardSettingService(IAwardSettingService awardSettingService) {
        this.awardSettingService = awardSettingService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="awardSettingDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,AwardSettingSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(awardSettingService.queryCount(search));
        List<AwardSetting> awardSettings= awardSettingService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("awardSettings", awardSettings);
        model.addAttribute("awardSettingSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String awardSettingDetailed(Model model,String key){
        AwardSetting awardSetting=new AwardSetting();
        if(!StringUtils.isEmpty(key)){
            awardSetting=awardSettingService.queryModel(key);
        }
        model.addAttribute("awardSetting", awardSetting);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,AwardSetting awardSetting){
        if(StringUtils.isEmpty(awardSetting.getId())){
            awardSetting.setId(CommonUtils.getKeyId());
            awardSetting.setCreateDate(DateUtils.getCurrent());
            awardSetting.setDeleteMark(YesOrNo.no);
            awardSetting.setCreateUserId(getLoginModel().getLoginId());
            awardSetting.setCreateUserName(getLoginModel().getLoginName());
            awardSetting.setModelStatus(ModelStatus.add);
        }else{
            awardSetting.setModifyDate(DateUtils.getCurrent());
            awardSetting.setModifyUserId(getLoginModel().getLoginId());
            awardSetting.setModifyUserName(getLoginModel().getLoginName());
            awardSetting.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(awardSettingService.applyChange(awardSetting)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("awardSetting", awardSetting);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(awardSettingService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
}
